package com.dongnan.hl7v2;

import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.model.*;
import ca.uhn.hl7v2.parser.CanonicalModelClassFactory;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.protocol.ReceivingApplicationExceptionHandler;
import ca.uhn.hl7v2.util.Terser;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

public class MyReceivingApplicationExceptionHandler implements ReceivingApplicationExceptionHandler {

    private  DataBaseCconnectPool dataBaseCconnectPool = DataBaseCconnectPool.getInstance();
    private  MyParaMap myParaMap = MyParaMap.getInstance();
    private  MyMessageProcessor myMessageProcessor = new MyMessageProcessor();
    HapiContext context = new DefaultHapiContext();
    CanonicalModelClassFactory mcf = new CanonicalModelClassFactory("2.4");
    PipeParser parser = null;

    public MyReceivingApplicationExceptionHandler() throws SQLException {

        parser = context.getPipeParser();
        parser.getParserConfiguration().setValidating(false);
        context.setModelClassFactory(mcf);
    }

    @Override
    public String processException(String s, Map<String, Object> map, String s1, Exception e) throws HL7Exception {
        myParaMap.cleanResult();
        s = s.replaceAll("\\\\r","");
        s = s.replaceAll("\\\\\\\\","\\\\");

        Message message = parser.parse(s);

        Terser terser = new Terser(message);
        String messageType = terser.get("/.MSH-9" );
        String messageID = terser.get("/.MSH-10");
        myMessageProcessor.processorMessage(messageType, message,myParaMap);
        try {
            myParaMap.getSQLList(messageType);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        String rawMessageSql = "INSERT INTO `raw_hl7_message`(`messageType`, `messageID`, `SendIP`, `SendPort`,`rawMessage`)\n" +
                " VALUES ('"+messageType+"', '"+messageID+"', '  ', ' ','"+s+"');";

        Message message1 = null;
        try {
            message1 = message.generateACK();
            Connection conn = dataBaseCconnectPool.getConnection();
            Statement st = conn.createStatement();
            st.execute(rawMessageSql);
            for (String string :myParaMap.sqlList){
//                System.out.println(string);
                st.execute(string);
            }
            message1 = message.generateACK();
            conn.close();
            st.close();
        } catch (IOException e1) {
            e.printStackTrace();
        } catch (SQLException e1) {
            e.printStackTrace();
        }
        return message1.encode();
    }


    public static void release(Connection conn, Statement st, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if (st != null) {
            try {
                st.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
